package com.mt.mtyx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.mtyx.entity.base.BaseEntity;
import com.mt.mtyx.entity.product.AttrGroup;
import com.mt.mtyx.product.mapper.AttrGroupMapper;
import com.mt.mtyx.product.service.AttrGroupService;
import com.mt.mtyx.vo.product.AttrGroupQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author WMT
 * @Date 2023/7/20 14:49
 */
@Service
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroup> implements AttrGroupService {

    @Override
    public IPage<AttrGroup> selectAll(Page<AttrGroup> attrGroupPage, AttrGroupQueryVo queryVo) {
        String name = queryVo.getName();
        LambdaQueryWrapper<AttrGroup> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like(AttrGroup::getName, name);
        }
        return baseMapper.selectPage(attrGroupPage, wrapper);
    }

    @Override
    public AttrGroup getDetails(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean existsByName(String name) {
        LambdaQueryWrapper<AttrGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrGroup::getName, name);
        return baseMapper.exists(wrapper);
    }


    @Override
    public int insert(AttrGroup attrGroup) {
        LocalDateTime now = LocalDateTime.now();
        attrGroup.setCreateTime(now);
        attrGroup.setUpdateTime(now);
        attrGroup.setIsDeleted(BaseEntity.NOT_DELETED);
        return baseMapper.insert(attrGroup);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int batchDelete(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    @Override
    public int update(AttrGroup attrGroup) {
        attrGroup.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(attrGroup);
    }

    @Override
    public List<AttrGroup> findAllList() {
        LambdaQueryWrapper<AttrGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AttrGroup::getId);
        return baseMapper.selectList(wrapper);
    }
}
